<?php

namespace app\common\model;

use app\common\enum\user\grade\log\ChangeType as ChangeTypeEnum;
use app\common\model\user\PointsLog as PointsLogModel;
use app\common\model\user\GradeLog as GradeLogModel;

/**
 * 用户模型类
 * Class User
 * @package app\common\model
 */
class User extends BaseModel
{
    protected $name = 'user';

    // 性别
    private $gender = ['未知', '男', '女'];

    /**
     * 关联会员等级表
     * @return \think\model\relation\BelongsTo
     */
    public function grade()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->belongsTo("app\\{$module}\\model\\user\\Grade");
    }

    /**
     * 关联收货地址表
     * @return \think\model\relation\HasMany
     */
    public function address()
    {
        return $this->hasMany('UserAddress');
    }

    /**
     * 关联收货地址表 (默认地址)
     * @return \think\model\relation\BelongsTo
     */
    public function addressDefault()
    {
        return $this->belongsTo('UserAddress', 'address_id');
    }

    /**
     * 显示性别
     * @param $value
     * @return mixed
     */
    public function getGenderAttr($value)
    {
        return $this->gender[$value];
    }

    /**
     * 获取用户信息
     * @param $where
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($where)
    {
        $filter = ['is_delete' => 0];
        if (is_array($where)) {
            $filter = array_merge($filter, $where);
        } else {
            $filter['user_id'] = (int)$where;
        }
        return self::get($filter, ['address', 'addressDefault', 'grade']);
    }

    /**
     * 累积用户的可用积分
     * @param $points
     * @param $describe
     * @param $scene_id
     * @param $scene
     * @param $shop_type
     * @param $scene_type
     * @return int|true
     * @throws \think\Exception
     */
    public function setIncPoints($points, $describe, $scene_id, $scene, $shop_type = 10, $scene_type = 10)
    {
        //类型(10积分商城20商城)
        // 新增积分变动明细
        PointsLogModel::add([
            'user_id' => $this['user_id'],
            'value' => $points,
            'scene_id' => $scene_id,
            'scene' => $scene,
            'describe' => $describe,
            'shop_type' => $shop_type,
            'scene_type' => $scene_type,
        ]);
        //增加用户总积分
        if ($points > 0) {
            $this->setInc('total_points', $points);
        }
        // 更新用户可用积分
        return $this->setInc('points', $points);
    }

    /**
     * 确认收货升级会员
     * @param $userInfo
     * @param $grade_id
     * @throws \think\exception\DbException
     */
    public static function setUserGrade($userInfo, $grade_id)
    {
        //获取用户现在的grade_id
        if ($userInfo['grade_id'] < $grade_id) {
            $model = self::detail($userInfo['user_id']);
            $model->save(['grade_id' => $grade_id]);
            //记录会员等级log日志
            $logData = [
                'user_id' => $userInfo['user_id'],
                'old_grade_id' => $userInfo['grade_id'],
                'new_grade_id' => $grade_id,
                'change_type' => 20,
            ];
            (new GradeLogModel)->save($logData);
        }
    }

}
